package com.hrg.rds.biz.comm;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.hrg.rds.biz.config.RdsConfiguration;
import com.hrg.seer.sdk.rbk.RbkResult;
import com.hrg.seer.sdk.rbk.RbkResultKind;
import com.hrg.seer.sdk.rbk.api.StatusApiEnum;
import com.hrg.seer.sdk.rbk.msg.status.*;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Set;

@Slf4j
public class StatusService {
	// 查询任意两点之间的路径
	private static RbkResult ROBOT_STATUS_GET_PATH_REQ(String ip, String fromStation, String toStation) {
		int apiNo = StatusApiEnum.ROBOT_STATUS_GET_PATH_REQ.getApiNo();
		GetPathReq req = GetPathReq.builder().source_id(fromStation).target_id(toStation).build();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, JSONUtil.toJsonStr(req), 0);
	}

	public static int getStatusPathCnt(String ip, String currentStation, String keyStation) {
		RbkResult rbkResult = StatusService.ROBOT_STATUS_GET_PATH_REQ(ip, currentStation, keyStation);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			GetPathRes res = JSONUtil.toBean(rbkResult.getResStr(), GetPathRes.class);
			if (res.getRet_code() == 0) {
				int cnt = res.getList().size();
				log.debug("获取路径 cnt = {}，ip = {}, currentStation = {}, keyStation = {}, list = {}", cnt, ip, currentStation, keyStation, res.getList());
				return cnt;
			}
			log.error("获取路径失败 ret_code = {}, err_msg = {}，ip = {}, currentStation = {}, keyStation = {}", res.getRet_code(), res.getErr_msg(), ip, currentStation, keyStation);
		}
		log.error("获取路径失败 kind = {}，ip = {}, currentStation = {}, keyStation = {}", rbkResult.getKind().name(), ip, currentStation, keyStation);
		return Integer.MAX_VALUE;
	}

	// 查询任务状态
	private static RbkResult ROBOT_STATUS_TASK_STATUS_PACKAGE_REQ(String ip, List<String> taskIds) {
		int apiNo = StatusApiEnum.ROBOT_STATUS_TASK_STATUS_PACKAGE_REQ.getApiNo();
		TaskStatusPackageReq req = CollUtil.isNotEmpty(taskIds) ?
				TaskStatusPackageReq.builder().task_ids(taskIds).build()
				: TaskStatusPackageReq.builder().build();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, JSONUtil.toJsonStr(req), 0);
	}
	public static TaskStatusPackage getTaskStatus(String ip, List<String> taskIds) {
		RbkResult rbkResult = StatusService.ROBOT_STATUS_TASK_STATUS_PACKAGE_REQ(ip, taskIds);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			TaskStatusPackageRes res = JSONUtil.toBean(rbkResult.getResStr(), TaskStatusPackageRes.class);
			if (res.getRet_code() == 0) {
				TaskStatusPackage taskStatusPackage = res.getTask_status_package();
				log.debug("查询任务状态 ip = {}, 正在执行的导航 source_name = {}, target_name = {}, info = {}", ip, taskStatusPackage.getSource_name(), taskStatusPackage.getTarget_name(), taskStatusPackage.getInfo());
				log.debug("task_status_list = {}", JSONUtil.toJsonStr(taskStatusPackage.getTask_status_list()));
				return taskStatusPackage;
			}
			log.error("查询任务状态失败 ret_code = {}, err_msg = {}，ip = {}", res.getRet_code(), res.getErr_msg(), ip);
		}
		log.error("查询任务状态失败 kind = {}，ip = {}", rbkResult.getKind().name(), ip);
		return null;
	}
}
